<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class DetailsController extends Controller
{

    //  
    public function index($id)
    {
    	
    	//获取评论表的gid
    	$data = DB::table('goods')->where('id',$id)->first();
        $id = $data->id;
        $commentGid=$data->cid;

        //查询商品详情goodsmain表
        $goodsmaindata = DB::table('goodsmain')->where('gid',$id)->get();
        $one = DB::table('goodsmain')->where('gid',$id)->first();
        //评价列表
        $list = DB::table('storecomment')
              ->join('storeuser','storeuser.userId','storecomment.commentUid')
              ->where('commentGid',$id)
              ->select('storecomment.commentPubTime','storecomment.commentBody','storeuser.userName','storeuser.userImg','storecomment.commentId','storecomment.commentStar')
              ->get();
         
           foreach ($list as $k => $v) {
                
                    $db = DB::table('stroereplay')
                        ->where('replayCid',$v->commentId)
                        ->select('replayTime','replayContent','replayer')
                        ->get();
                    if ( $db ) {
                    
                        foreach($db as $m => $n){
                                  $list[$k]->replayTime = $n->replayTime;
                                  $list[$k]->replayContent = $n->replayContent; 
                                  $list[$k]->replayer = $n->replayer;
                        }
                    }
             }
    
     
    	 return view('home.goods.details',[ 'comment' => $list, 'goodsId' => $id],compact('goodsmaindata','one'));
    }

    //咨询提交
    public function addConsult(Request $request)
    {
        
         //自定义错自误信息
        $message = [
            'required' => '咨询内容不能为空',
        ];

        $this->validate($request, [
            'coContent' => 'required'
            
        ],$message);

        $data = $request->except('_token');
        $data['uId'] = session('user')->userId;
        $data['coTime'] = time();
// dd($data);
        //存入数据
        $row = DB::table('consult')->insertGetId($data);
        if ( $row > 0 ) {
            return  back()->with('msg','数据提交成功');
         }

        
    }

       //咨询数据库遍历
       public function consultAjax(Request $request)
       {

            $gId = $request->input('gId');
            $coType = $request->input('coType');
          $pages = $request->input('pages');

            if ( $coType == '5' ) {

                //获取全部咨询数据
                $conList = DB::table('consult')
                    ->join('storeuser','consult.uId','storeuser.userId')
                    ->where('gid',$gId)
                    ->select('consult.coContent','consult.coTime','storeuser.userName','consult.consultId')
              ->skip($pages)->take(3)->get();
            } else {
                //获取分类咨询数据
            $conList = DB::table('consult')
                    ->join('storeuser','consult.uId','storeuser.userId')
                    ->where([
                             ['gid', '=', $gId],
                             ['coType', '=', $coType],
                            ])
                    ->select('consult.coContent','consult.coTime','storeuser.userName','consult.consultId')
                    // ->get();
                ->skip($pages)->take(3)->get();
                    // ->paginate(2);
            }

            // 查询咨询回复的内容
            foreach ($conList as $key => $value) {

                $conList[$key]->coTime = date('Y-m-d H:i:s',$value->coTime);
                $db = DB::table('consultre')
                    ->join('storeadmin','consultre.coReUid','storeadmin.adminId')
                    ->where('coReCid',$value->consultId)
                    ->select('consultre.coReContent','consultre.coReTime','storeadmin.adminName')
                    ->get();

                if ( $db ) {

                    foreach ($db as $m => $n) {
                        //把回复的内容存入到一个变量 
                        $conList[$key]->adminName = $n->adminName;
                        $conList[$key]->coReTime = date('Y-m-d H:i:s',$n->coReTime);
                        $conList[$key]->coReContent = $n->coReContent;
                    } 
                }
            }
        
        echo json_encode($conList);
      
       }    
   

}
